<!-- -->
<template>
  <div class="all">
    <el-container>
      <el-main>
        <div class="content">
          <div class="goodsList" v-for="item in List" :key="item.id">
            <img :src="item.imageUrl" alt="" />
            <div style="width:100%">
              <span class="textSetting">{{ item.name }}</span>
            </div>
            <p class="textSetting">{{ item.description }}</p>
            <div class="buttonList">
              <el-button size="small" type="danger" @click="addCart(item.id)"
                >加入购物车</el-button
              >
              <el-button
                size="small"
                type="primary"
                @click="goToDetail(item.id)"
                >查看详情</el-button
              >
            </div>
          </div>
        </div>
        <div class="footer">
          <span
            >东莞理工学院17级实训 @第六组 &nbsp;&nbsp; &nbsp;&nbsp;
            _版权所有</span
          >
        </div>
      </el-main>
    </el-container>
  </div>
</template>

<script>
export default {
  name: "All",
  data() {
    return {
      userId: "",
      List: []
    };
  },
  created() {
    this.getAllGood();
    this.userId = sessionStorage.getItem("userId");
  },

  components: {},

  filters: {},

  computed: {},

  methods: {
    Success(index) {
      this.$message.success(index);
    },
    Fail(index) {
      this.$message.error(index);
    },
    getAllGood() {
      this.axios
        .get("/api/goods")
        .then(res => {
          console.log(res.data);
          if (res.data.code === 200) {
            let result = res.data.data.list;
            let bufferList = [];
            for (let key in result) {
              let i = new Object();
              i.id = result[key].id;
              i.imageUrl = result[key].imgList[0].url;
              i.name = result[key].name;
              i.description = result[key].description;
              i.price = result[key].price;
              bufferList.push(i);
            }
            this.List = bufferList;
          }
        })
        .catch(error => {
          console.log(error);
        });
    },
    addCart(id) {
      this.userId = sessionStorage.getItem("userId");
      this.axios
        .post("/api/cart", {
          uid: this.userId,
          gid: id
        })
        .then(res => {
          // console.log(res.data);
          if (res.data.code === 200) {
            this.Success("添加至购物车成功，请前往购物车查看");
          } else {
            this.Fail("添加至购物车失败");
          }
        })
        .catch(error => {
          console.log(error);
        });
    },
    goToDetail(id) {
      sessionStorage.setItem("goodId", id);
      this.Success("页面将在三秒后跳转");
      setTimeout(() => {
        this.$router.push("/goods/details");
      }, 3000);
    }
  }
};
</script>
<style scoped>
.all {
  height: 100%;
  width: 100%;
  padding-top: 60px;
  box-sizing: border-box;
}
.el-container {
  height: 100%;
  width: 100%;
  background-color: #fff;
}

.el-header,
.el-footer {
  background-color: white;
  color: #333;
  text-align: center;
  line-height: 60px;
}
.el-main {
  background-color: #e9eef3;
  color: #333;
  text-align: center;
  /* line-height: 30px; */
  overflow-x: hidden;
  padding: 0;
}
.el-main::-webkit-scrollbar {
  display: none;
}
/* 搜索框样式 */
.el-header .el-input {
  width: 50%;
  margin-left: auto;
  margin-right: auto;
  border-color: #0d1a2e;
}

.content {
  width: 70%;
  min-height: 100%;
  margin: 0;
  padding: 0;
  margin-left: auto;
  margin-right: auto;
  margin-top: 28px;
  background-color: white;
  text-align: left;
}
.goodsList {
  width: 25%;
  height: 50vh;
  padding: 28px;
  box-sizing: border-box;
  display: inline-block;
  text-align: center;
}
.goodsList img {
  width: 70%;
  height: 20vh;
}
.goodsList:hover {
  box-shadow: 0 2px 10px rgb(0, 0, 0, 0.3);
}
.goodsList:hover div {
  display: block;
}
.goodsList:hover p {
  display: none;
}
.textSetting {
  display: inline-block;
  width: 80%;
  margin-left: auto;
  margin-right: auto;
  white-space: nowrap;
  text-overflow: ellipsis;
  overflow: hidden;
}
.buttonList {
  width: 100%;
  height: 60px;
  margin-top: 28px;
  display: none;
}
/* .buttonList:hover{
  display: block;
} */
.footer {
  width: 100%;
  height: 60px;
  margin: 0;
  margin-top: 60px;
  padding: 0;

  background-color: white;
  line-height: 60px;
  text-align: right;
}
.footer span {
  margin-right: 28px;
}
</style>